# -*- coding: utf-8 -*-
import pytest
import os
from common.app_info import AndroidApp
from utils.PathUtil import LOG_DEVICE_PATH
from utils.pyqhipc_serial import QhIpcSerial
from utils.serial_util import SerialUtil
from utils.time_utils import get_datetime, YMDHMS_


def pytest_addoption(parser):
    parser.addoption("--app_sys", action="store", default="android", help="手机系统: android 或 ios")
    parser.addoption("--app_tag", action="store", default="smart", help="表示测试app: smart智慧生活  cloudsmart云智连")

    parser.addoption("--app_interval", action="store", default="1", help="测试后停多长时间进行下次测试: 1")
    parser.addoption("--wifi_name", action="store", default="", help="wifi名")  # 360test-caros-002-5G
    parser.addoption("--wifi_pwd", action="store", default="", help="wifi密码")  # qwerasdf31
    parser.addoption("--pk", action="store", help="本次测试设备的PK")  # 5081e5dd2d6a
    parser.addoption("--bind_type", action="store", help="绑定方式")
    parser.addoption("--wakeup_time", action="store", help="ota升级前等待时间")

    parser.addoption("--lock_wakeup_com", action="store", default='-1', help="唤醒门锁-继电器串口号")
    parser.addoption("--lock_wakeup_bps", action="store", default=9600, help="唤醒门锁-继电器波特率")

    parser.addoption("--device_com", action="store", default='-1', help="设备串口号")
    parser.addoption("--device_bps", action="store", default=115200, help="设备波特率")
    parser.addoption("--device_account", action="store", default='', help="设备串口登录账号")
    parser.addoption("--device_pwd", action="store", default='', help="设备串口登录密码")
    parser.addoption("--need_login", action="store", default='否', help="串口是否需要登录 是 否")


@pytest.fixture(scope="session")
def device_pk(request):
    pk = request.config.getoption("--pk")
    yield pk


@pytest.fixture(scope="session")
def bind_type(request):
    bind_type = request.config.getoption("--bind_type")
    yield bind_type


@pytest.fixture(scope="session")
def app_interval(request):
    app_interval = request.config.getoption("--app_interval")
    yield app_interval


@pytest.fixture(scope="session")
def wakeup_time(request):
    wakeup_time = request.config.getoption("--wakeup_time")
    yield wakeup_time


@pytest.fixture(scope="session")
def ssid_info(request):
    wifi_name = convert_space(request.config.getoption("--wifi_name"))
    wifi_pwd = convert_space(request.config.getoption("--wifi_pwd"))
    yield wifi_name, wifi_pwd


@pytest.fixture(scope="session")
def device_serial(request):
    device_com = request.config.getoption("--device_com")
    device_bps = request.config.getoption("--device_bps")
    device_account = request.config.getoption("--device_account")
    device_pwd = request.config.getoption("--device_pwd")
    need_login = request.config.getoption("--need_login")

    if device_com == '-1':
        yield
    else:
        if not os.path.exists(LOG_DEVICE_PATH):
            os.mkdir(LOG_DEVICE_PATH)
        # DEVICE_LOG_PATH = os.path.join(LOG_DEVICE_PATH, get_datetime(YMDHMS_) + '_' + device_com + '.txt')
        DEVICE_LOG_PATH = os.path.join(LOG_DEVICE_PATH, get_datetime(YMDHMS_) + '_' + '.txt')
        local_log = open(DEVICE_LOG_PATH, "w", encoding='utf8')

        def _output(msg):
            local_log.write(msg + "\n")
            local_log.flush()
        if need_login == '否':
            # 个别设备登录账号密码 不同，自行修改
            com = QhIpcSerial(device_com, device_bps, username=device_account, password=device_pwd, debug_printer=_output, if_need_login=False)
        else:
            # 个别设备登录账号密码 不同，自行修改
            com = QhIpcSerial(device_com, device_bps, username=device_account, password=device_pwd, debug_printer=_output)
        print(f'串口连接成功, 本次固件日志保存在  {DEVICE_LOG_PATH}')
        yield com


@pytest.fixture(scope="session")
def lock_wakeup_relay(request):
    lock_wakeup_com = request.config.getoption("--lock_wakeup_com")
    lock_wakeup_bps = request.config.getoption("--lock_wakeup_bps")

    if 'com' in lock_wakeup_com or 'COM' in lock_wakeup_com or 'dev' in lock_wakeup_com:
        yield SerialUtil(lock_wakeup_com, lock_wakeup_bps)
    else:
        yield


@pytest.fixture(scope="session")
def executeDriver(request):
    app_system = request.config.getoption('--app_sys')
    app_tag_name = request.config.getoption('--app_tag')
    app_interval = request.config.getoption('--app_interval')

    app_sys_plat = app_system.lower()

    AndroidApp[app_tag_name.lower()]["appPackage"]

    yield app_sys_plat, app_tag_name, app_interval


def convert_space(content):
    try:
        return content.replace('@@@', ' ')
    except:
        pass
